package com.mmy.acassistant.model.main.presenter

import com.blankj.utilcode.util.AppUtils
import com.google.gson.reflect.TypeToken
import com.mmy.acassistant.model.main.view.MainView
import com.mmy.frame.base.able.OnLoadDataListener
import com.mmy.frame.base.mvp.IPresenter
import com.mmy.frame.data.bean.BaseBean
import com.mmy.frame.data.bean.IBean
import com.mmy.frame.data.bean.VersionBean
import com.mmy.frame.helper.CommentHelper
import java.lang.reflect.Type
import javax.inject.Inject


/**
 * @创建者     lucas
 * @创建时间   2017/12/25 0025 11:48
 * @描述          TODO
 */
class MainPresenter @Inject constructor() : IPresenter<MainView>(), CommentHelper {

    /**
     * 检查版本
     */
    fun checkVersion() {
        mV.showLoading()
        mM.wrapRequest(object : OnLoadDataListener<BaseBean<VersionBean>>() {
            override fun onSuccess(body: String, data: BaseBean<VersionBean>?, iBean: IBean) {
                if (iBean.status == 1 && data != null && AppUtils.getAppVersionCode() < data.data.versionCode) {
                    mV.onVersionChange()
                }
            }

            override fun onFailed(body: String?, t: Throwable) {
            }

            override fun onCompleted() {
                mV.hidLoading()
            }

            override fun getBeanType(): Type {
                return object : TypeToken<BaseBean<VersionBean>>() {}.type
            }
        },mM.apiService.checkVersion())
    }
}